package com.hm.ibsm.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Component;

import com.hm.common.def.DataStatusDef;
import com.hm.common.exception.ErrorCode;
import com.hm.common.exception.ServiceException;
import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.PageInfo.PageParam;
import com.hm.common.util.CommonUtil;
import com.hm.ibsm.def.enums.DictTypeEnums;
import com.hm.ibsm.domain.mg.DictDomain;
import com.hm.ibsm.domain.mg.QDictDomain;
import com.hm.ibsm.repositry.IDictRepositry;
import com.hm.ibsm.util.BeanUtil;
import com.hm.ibsm.util.R.DB;
import com.hm.ibsm.vo.dict.DictTypeValueVo;
import com.hm.ibsm.vo.dict.DictVo;
import com.querydsl.core.types.dsl.BooleanExpression;

/**
 * @author shishun.wang
 * @date 2017年3月7日 上午10:28:21
 * @version 1.0
 * @describe
 */
@Component
@Transactional
public class DictService {

	@Autowired
	private IDictRepositry dictRepositry;

	public void addDict(DictVo dictVo) {
		// TODO Auto-generated method stub

	}

	public void updateDict(String dictId, DictVo dictVo) {
		// TODO Auto-generated method stub

	}

	public void deleteDict(String dictId) {
		// TODO Auto-generated method stub

	}

	public PageInfo<DictVo> query(PageParam page, DictTypeEnums dictType, String code) {
		PageRequest pageable = new PageRequest(page.getPage(), page.getSize(), new Sort(Direction.DESC, new String[] { DB.DEFAULT_SORT }));
		QDictDomain qDictDomain = QDictDomain.dictDomain;
		BooleanExpression predicate = qDictDomain.status.eq(DataStatusDef.ENABLE);
		if (CommonUtil.isNotEmpty(dictType)) {
			predicate = predicate.and(qDictDomain.dictType.eq(dictType));
		}
		if (CommonUtil.isNotEmpty(code)) {
			predicate = predicate.and(qDictDomain.code.like(code));
		}

		Page<DictDomain> pageContent = dictRepositry.findAll(predicate, pageable);
		List<DictVo> vos = new ArrayList<DictVo>();
		pageContent.getContent().stream().forEach(dict -> {
			DictVo vo = new DictVo();
			BeanUtil.copyProperties(dict, vo);
			vos.add(vo);
		});
		return new PageInfo<DictVo>(vos, pageContent.getTotalPages(), pageContent.getTotalElements());
	}

	public List<DictTypeValueVo> getDictTypeValues(DictTypeEnums dictType) {
		if (CommonUtil.isEmpty(dictType)) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_NULL, "字典类型不能为空");
		}

		List<DictTypeValueVo> vos = new ArrayList<DictTypeValueVo>();
		dictRepositry.findAll(QDictDomain.dictDomain.dictType.eq(dictType), new Sort("level")).forEach(dict -> {
			DictTypeValueVo vo = new DictTypeValueVo();
			BeanUtil.copyProperties(dict, vo);
			vos.add(vo);
		});
		return vos;
	}

	public DictTypeValueVo getDictValue(String code) {
		if (CommonUtil.isEmpty(code)) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_NULL, "字典编码不能为空");
		}
		DictDomain dict = dictRepositry.findOne(QDictDomain.dictDomain.code.eq(code));
		if (CommonUtil.isEmpty(dict)) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND, "字典编码" + code);
		}
		DictTypeValueVo vo = new DictTypeValueVo();
		BeanUtil.copyProperties(dict, vo);
		return vo;
	}

	public List<DictTypeValueVo> getDictValues(String code) {
		if (CommonUtil.isEmpty(code)) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_NULL, "字典编码不能为空");
		}
		List<DictTypeValueVo> vos = new ArrayList<DictTypeValueVo>();
		dictRepositry.findAll(QDictDomain.dictDomain.code.eq(code), new Sort("level")).forEach(dict -> {
			DictTypeValueVo vo = new DictTypeValueVo();
			BeanUtil.copyProperties(dict, vo);
			vos.add(vo);
		});
		return vos;
	}

}
